<?php

namespace App\Models;

use App\Enums\AvailableStatus;
use App\Enums\OrderStatus;
use BenSampo\Enum\Traits\CastsEnums;
use DateTimeInterface;
use Illuminate\Foundation\Auth\User as AuthUser;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * @mixin IdeHelperFrontendUser
 */
class FrontendUser extends AuthUser implements JWTSubject
{
    use CastsEnums;

    protected $casts = [
        'status' => AvailableStatus::class,
    ];

    // 除 id, created_at, updated_at 外的其它字段都是 fillable
    protected $guarded = ['id', 'created_at', 'updated_at'];

    protected $hidden = ['password'];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function channel()
    {
        return $this->belongsTo(Channel::class, 'channel_id');
    }

    public function parent()
    {
        return $this->belongsTo(FrontendUser::class, 'parent_id');
    }

    public function auth()
    {
        return $this->hasOne(UserAuth::class, 'user_id');
    }

    public function agent()
    {
        return $this->belongsTo(Agent::class);
    }

    public function address()
    {
        return $this->hasOne(UserAddress::class, 'user_id');
    }

    public function order()
    {
        return $this->hasMany(Order::class, 'user_id');
    }

    public function paid_order()
    {
        return $this->hasMany(Order::class, 'user_id')->where('order_status', OrderStatus::PAID);
    }

    public function wallet()
    {
        return $this->hasOne(UserWallet::class, 'user_id');
    }

    public function punish_log()
    {
        return $this->hasOne(Punish::class, 'user_id')->where('punish_status', 1);
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
}
